package com.cx.controller;

import com.cx.pojo.Carts;
import com.cx.pojo.GoodDetail;
import com.cx.pojo.Users;
import com.cx.service.GoodDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author ：zhangjie
 * @Description :
 */
@Controller
@RequestMapping("/good")
public class GoodDetailsController {
    @Autowired
    GoodDetailService goodDetailService;

    /**
     * 普通商品信息
     */
    @RequestMapping("/detail")
    public String GoodDetails(HttpServletRequest request,Integer id){
        GoodDetail goodDetail;
        goodDetail=goodDetailService.selectById(id);
        request.getSession().setAttribute("goodDetail",goodDetail);
        return "gooddetail/gooddetail";
    }

    /**
     * 秒杀商品信息
     */
    @RequestMapping("/seckll")
    public String seckllGoodDetails(HttpServletRequest request,Integer id){
        GoodDetail goodDetail;
        goodDetail=goodDetailService.selectBySeckillId(id);
        request.getSession().setAttribute("goodDetail",goodDetail);
        return "gooddetail/gooddetail";
    }

    /**
     * 判断是否登录
     */
    @RequestMapping("/user")
    @ResponseBody
    public Integer user(HttpSession session){
        Users user = (Users) session.getAttribute("user");
        if (user == null) {
            return 0;
        }
        return 1;
    }
    /**
     * 判断是否已经收藏
     */
    @RequestMapping("/collect")
    @ResponseBody
    public List<Integer> collect(HttpSession session){
        Users user = (Users) session.getAttribute("user");
        List<Integer> list = null;
        if (user == null) {
            return list;
        }
        list=goodDetailService.selectGoodId(user.getId());
        System.out.println(list);
        return list;
    }

    /**
     * 加入收藏
     */
    @RequestMapping("/addCollect")
    @ResponseBody
    public Integer addCollect(Integer goodId,HttpSession session){
        Users user = (Users) session.getAttribute("user");
        if (user == null) {
            return -1;
        }
        int attfectRows=goodDetailService.insertCollection(user.getId(),goodId);
        return attfectRows;
    }

    /**
     * 删除收藏
     */
    @RequestMapping("/deleteCollect")
    @ResponseBody
    public Integer deleteCollect(Integer goodId,HttpSession session){
        Users user = (Users) session.getAttribute("user");
        if (user == null) {
            return -1;
        }
        int attfectRows=goodDetailService.deleteByGoodId(goodId);
        return attfectRows;
    }

    /**
     * 加入购物车
     */
    @RequestMapping("/cart")
    @ResponseBody
    public int cart(Integer goodId,Integer goodNum, HttpSession session){
        Users user = (Users) session.getAttribute("user");
        if (user == null) {
            return -1;
        }
        int i=0;
        List<Carts> cartsList=goodDetailService.selectByUserId(user.getId());
        for (i = 0; i <cartsList.size() ; i++) {
            if(cartsList.get(i).getGoodId()==goodId){
                cartsList.get(i).setGoodNum(cartsList.get(i).getGoodNum()+goodNum);
                int attfectRows =goodDetailService.updateByGoodId(cartsList.get(i));
                return attfectRows;
            }
        }
        if(i==cartsList.size()) {
            Carts carts = new Carts();
            carts.setUserId(user.getId());
            carts.setGoodId(goodId);
            carts.setGoodNum(goodNum);
            int attfectRows = goodDetailService.insert(carts);
            return attfectRows;
        }
        return 0;
    }
}
